package algorithm_optimal_solution.LinkedList;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//反转单向链表和双向链表：A->B->C->D链表。1.A  2.B->A  3.C->B->A
public class ReverseLinkedList {

    static class DoubleNode{
        private int value;
        private DoubleNode lastNode;
        private DoubleNode nextNode;

        public DoubleNode(int value, DoubleNode lastNode, DoubleNode nextNode) {
            this.value = value;
            this.lastNode = lastNode;
            this.nextNode = nextNode;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public DoubleNode getLastNode() {
            return lastNode;
        }

        public void setLastNode(DoubleNode lastNode) {
            this.lastNode = lastNode;
        }

        public DoubleNode getNextNode() {
            return nextNode;
        }

        public void setNextNode(DoubleNode nextNode) {
            this.nextNode = nextNode;
        }
    }

    static class Node{
        private int value;
        private Node node;

        public Node(int value,Node node){
            this.value = value;
            this.node = node;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getNode() {
            return node;
        }

        public void setNode(Node node){
            this.node = node;
        }
    }

    public void reverseFloatLinkedList(Node head){
        Node pre = null;
        Node next = null;
        while(head != null){//假设a,b,c为原链表第1,2,3个
            next = head.getNode();//暂存c
            head.setNode(pre);//当前链表中b的下一个设置为a
            pre = head;//对下一次检查来说，上一个为b
            head = next;//下一次检查为暂存的c
        }
        StringBuilder sb = new StringBuilder();
        while(pre != null){
            sb.append(pre.getValue()).append(" ");
            pre = pre.getNode();
        }
        System.out.print(sb);
    }

    public void reverseDoubleLinkedList(DoubleNode head){
        DoubleNode pre = null;
        DoubleNode next = null;
        while(head != null){
            next = head.getNextNode();
            head.setNextNode(pre);
            head.setLastNode(next);
            pre = head;
            head = next;
        }
        StringBuilder sb = new StringBuilder();
        while(pre != null){
            sb.append(pre.getValue()).append(" ");
            pre = pre.getNextNode();
        }
        System.out.print(sb);
    }

    public static void main(String[] args)throws IOException {
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        int len = Integer.parseInt(scanner.readLine());
        String[] numbers = scanner.readLine().split(" ");
        Node node = null;
        for(int i = len - 1;i >= 0;i--){
            node = new Node(Integer.parseInt(numbers[i]),node);
        }
        new ReverseLinkedList().reverseFloatLinkedList(node);
        int doubleLen = Integer.parseInt(scanner.readLine());
        String[] doubleNumbers = scanner.readLine().split(" ");
        DoubleNode doubleNode;
        doubleNode = new DoubleNode(Integer.parseInt(doubleNumbers[doubleLen - 1]),null,null);
        for(int i = doubleLen - 2;i >= 0;i--){
            doubleNode = new DoubleNode(Integer.parseInt(doubleNumbers[i]),null,doubleNode);
            doubleNode.getNextNode().setLastNode(doubleNode);
        }
        System.out.println();
        new ReverseLinkedList().reverseDoubleLinkedList(doubleNode);
    }
}
